package edu.scdx.controller;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.scdx.common.util.JsonUtil;
import edu.scdx.entity.Cart;
import edu.scdx.entity.Goods;
import edu.scdx.entity.Order;
import edu.scdx.service.GoodsService;
import edu.scdx.service.UserInfoService;
import edu.scdx.service.OrderService;


@Controller
@RequestMapping("/order")
public class OrderController {
	
	 @Autowired
	 private OrderService orderService;
	 
	 @Autowired
	 private GoodsService goodsService;
	 
	 @Autowired
	 private UserInfoService userInfoService;

	public OrderService getOrderService() {
		return orderService;
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
	
	@RequestMapping("/buy.do")
    public String addOrder(Model model,HttpSession session){	
									
		int id=(int) session.getAttribute("id");
		
		String userTel=(String) session.getAttribute("userTel");
		
		Goods good=goodsService.getGoodById(id);
		
		String name=good.getName();
		
		String type=good.getType();
		
		String author=good.getAuthor();
		
		String publisher=good.getPublisher();
		
		String description=good.getDescription();
		
		double price=good.getPrice();
		
		double kou=good.getKou();
		
		Integer state=0;
		
		Integer comment=0;
		
		orderService.addOrder(userTel,id,name,type,author,publisher,description,price,kou,state,comment);
		
		int rankAdd=1;
		
		userInfoService.updateMoney(userTel,price,rankAdd);
		
		model.addAttribute("orderGoodInfo", good);
		
    	return "address";
    }
	
	
	@RequestMapping("/cartBuy.do")
    public String addCartOrder(Model model,HttpSession session,Integer id){	
									
				
		String userTel=(String) session.getAttribute("userTel");
		
		Goods good=goodsService.getGoodById(id);
		
		String name=good.getName();
		
		String type=good.getType();
		
		String author=good.getAuthor();
		
		String publisher=good.getPublisher();
		
		String description=good.getDescription();
		
		double price=good.getPrice();
		
		double kou=good.getKou();
		
		Integer state=0;
		
		Integer comment=0;
		
		orderService.addOrder(userTel,id,name,type,author,publisher,description,price,kou,state,comment);
		
		int rankAdd=1;
		
		userInfoService.updateMoney(userTel,price,rankAdd);
		
		model.addAttribute("orderGoodInfo", good);
		
    	return "address";
    }
	

	public GoodsService getGoodsService() {
		return goodsService;
	}

	public void setGoodsService(GoodsService goodsService) {
		this.goodsService = goodsService;
	}

	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
	
	@RequestMapping("/seeOrder.do")
    public String seeOrder(Model model,HttpSession session){	
									
				
		String userTel=(String) session.getAttribute("userTel");
		
		int state=0;
		
		List<Order> orders = orderService.getOrderByState(userTel,state);
		
		model.addAttribute("stateOrders",orders);
		
		
    	return "order";
    }
	 
	
	@RequestMapping("/getOrderByState.do")
    public String getOrderByState(Model model,HttpSession session,Integer state){	
									
				
		String userTel=(String) session.getAttribute("userTel");
		
		List<Order> orders = orderService.getOrderByState(userTel,state);
		
		model.addAttribute("stateOrders",orders);
		
    	return "stateOrders";
    }
	
	
//	getOrderByComment
	
	
	
	
	
	@RequestMapping("/getOrderByComment.do")
    public String getOrderByComment(Model model,HttpSession session,Integer comment){	
									
				
		String userTel=(String) session.getAttribute("userTel");
		
		List<Order> orders = orderService.getOrderByComment(userTel,comment);
		
		model.addAttribute("commentOrders",orders);
		
		
		if(comment==0){
			return "noCommentOrders";
		}
		else {
			return "haveCommentOrders";
		}
		
		
		
    	
    }
	
	
	
	
	
	@RequestMapping("/deleteOrder.do")
    public String deleteOrder(Model model,HttpSession session,Integer id){	
									
				
		String userTel=(String) session.getAttribute("userTel");
		
		orderService.deleteOrder(userTel,id);
		
    	return "deleteSucceed";
    }
	
	
	@RequestMapping("/updateComment.do")
    public String updateComment(Model model,HttpSession session,Integer id){	
									
				
		String userTel=(String) session.getAttribute("userTel");
		
		Integer add=1;
		
		orderService.updateComment(userTel,id,add);
		
		Integer jifen=10;
		
		userInfoService.updateScore(userTel,jifen);
		
		
    	return "commentSucceed";
    }
	
	
	@RequestMapping("/sellGoods.do")
    public String getAllNotSold(Model model,HttpSession session){	
									
		Integer state=0;
		
		List<Order> orders = orderService.getAllNotSold(state);
		
		model.addAttribute("nots",orders);
		
    	return "sellGoods";
    }
	
	@RequestMapping("/deal.do")
    public String updateState(Model model,HttpSession session,String tel,Integer id){	
									
		Integer state=1;
		
		orderService.updateState(tel,id,state);
		
    	return "dealOk";
    }
	
	
	 

}
